class Observer {
    update(sth) {
        console.log(sth)
    }
}

class Subject {
    constructor() {
        this.observers = [];
        this.state = 'xxx';
    }

    subscribe(observer) {
        this.observers.push(observer);
    }

    publish(str) {
        this.state = str;
        this.observers.forEach(observer => {
            observer.update(str);
        })
    }
}

let o1 = new Observer();
let o2 = new Observer();
let s = new Subject();

s.subscribe(o1)
s.subscribe(o2)

s.publish('new')